//
//  LJIssueDelImageCell.m
//  LajinLive
//
//  Created by lajin on 16/4/7.
//  Copyright © 2016年 lajin. All rights reserved.
//

#import "IGIssueDelImageCell.h"

static CGFloat maxScale = 3.0;
static CGFloat minScale = 1.0;

@interface IGIssueDelImageCell ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    CGFloat _currentScale;
}
@end
@implementation IGIssueDelImageCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.contentView.frame];
        _scrollView.userInteractionEnabled = YES;
        _scrollView.maximumZoomScale = maxScale;
        _scrollView.minimumZoomScale = minScale;
        _scrollView.delegate = self;
        _currentScale = 1.0;
        [self.contentView addSubview:_scrollView];
        
        _imageView = [[UIImageView alloc] initWithFrame:self.contentView.frame];
        _imageView.userInteractionEnabled = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_scrollView addSubview:_imageView];
        UITapGestureRecognizer *doubelGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleGesture:)];
        doubelGesture.numberOfTapsRequired=2;
        [_imageView addGestureRecognizer:doubelGesture];
        
    }
    return self;
}

//#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    _currentScale=scale;
}

-(void)doubleGesture:(UIGestureRecognizer *)sender
{
    
    //当前倍数等于最大放大倍数
    //双击默认为缩小到原图
    if (_currentScale==maxScale) {
        _currentScale=minScale;
        [_scrollView setZoomScale:_currentScale animated:YES];
        return;
    }
    //当前等于最小放大倍数
    //双击默认为放大到最大倍数
    if (_currentScale==minScale) {
        _currentScale=maxScale;
        [_scrollView setZoomScale:_currentScale animated:YES];
        return;
    }
    
    CGFloat aveScale =minScale+(maxScale-minScale)/2.0;//中间倍数
    
    //当前倍数大于平均倍数
    //双击默认为放大最大倍数
    if (_currentScale>=aveScale) {
        _currentScale=maxScale;
        [_scrollView setZoomScale:_currentScale animated:YES];
        return;
    }
    
    //当前倍数小于平均倍数
    //双击默认为放大到最小倍数
    if (_currentScale<aveScale) {
        _currentScale=minScale;
        [_scrollView setZoomScale:_currentScale animated:YES];
        return;
    }
}


@end
